package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 金帮嘉诚 @date 2021-08-14 星期六 17:18
 */
@Service
public class RightsServiceImpl implements RightsService {
    @Autowired
    private RightsMapper rightsMapper;

    @Override
    public List<Rights> getRightsList() {
        //获取整个rights表
        List<Rights> rights = rightsMapper.selectList(null);
        //获取一级菜单
        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<Rights> rights1 = rightsMapper.selectList(queryWrapper);
        //遍历一级菜单
        for (Rights r1 : rights1
        ) {//一级菜单项的子菜单表 twoList
            List<Rights> twoList = new ArrayList<>();
            //遍历2级菜单 如果 当前r2属于 当前r1 的 子菜单项 (即r2的父id=r1的id),就把r2放入twoList
            for (Rights r2 : rights
            ) {
                if (r2.getParentId().equals(r1.getId())) {
                    //符合条件 则加入twoList
                    twoList.add(r2);
                }
            }
            //遍历完后 将twoList放入一级菜单项
            r1.setChildren(twoList);
        }

        return rights1;
    }
}
